const useDict = useDictStore();
import previewUrl from "@/utils/previewUrl";
import { isArray } from "lodash-es";
// 获取当前日期一周的时间
export function getCurrentWeek() {
	const date = new Date();
	//日期格式化
	const dateFormat = (date) => {
		return (
			date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate()
		);
	};

	//一天的毫秒数
	const ondDayTime = 86400000;
	//-1是因为要把当天的毫秒数去掉
	let day = date.getDay() - 1;
	let firstDay = "";
	//如果<0，就是周末，所以取6
	day < 0 ? (day = 6) : "";
	//周一
	firstDay = new Date(dateFormat(new Date(date.getTime() - day * ondDayTime)));
	const week = [];
	const chineseWeek = ["一", "二", "三", "四", "五", "六", "天"];
	for (let i = 0; i <= 6; i++) {
		const str = dateFormat(new Date(firstDay.getTime() + i * ondDayTime));
		week.push({
			//可换成星期
			chinese: "周" + chineseWeek[i],
			day: i,
			date: str.substring(str.indexOf("-") + 1, str.length),
			year: new Date(firstDay.getTime() + i * ondDayTime).getFullYear(),
		});
	}
	return week; //结果
}
// 拨打电话
export function dialNumber(tel) {
	uni.showModal({
		title: "提示",
		content: "确定拨打电话吗",
		success: function (res) {
			if (res.confirm) {
				window.location.href = "tel:" + tel;
			} else if (res.cancel) {
			}
		},
	});
}

// 获取状态对应颜色
export function getStatusColor(status) {
	// 获取状态枚举
	const AppointmentStatusEnum = useDict.getData("RESERVATION_STATUS");
	let color = "#409eff";
	switch (status) {
		// 已驳回
		case AppointmentStatusEnum.REFUSED.value:
			color = "#ea8291";
			break;

		default:
			color = "#409eff";
			break;
	}
	return color;
}

/**
 * 获取枚举对应数据
 * @param {*} list
 * @param {*} value
 */
export function getLabel(data, value) {
	if (isArray(data)) {
		return data.find((e) => {
			return e.value === value;
		})?.label;
	} else {
		return data[value]?.label;
	}
}

// 格式化参观时间
export function formatVisitTime(visitDate, visitTime) {
	return dayjs(visitDate).format("YYYY-MM-DD") + "\xa0" + visitTime;
}

// 格式化参观时长
export function formatVisitDuration(value) {
	return value.split("-")[0] + "小时" + value.split("-")[0] + "分";
}

// 判断文件类型是否为图片
export function getsTheFileType(filePath) {
	const index = filePath.lastIndexOf(".");
	const ext = filePath.substring(index + 1);
	return (
		["png", "jpg", "jpeg", "bmp", "gif", "webp", "psd", "svg", "tiff"].indexOf(
			ext.toLowerCase(),
		) !== -1
	);
}

// 预览
export function preview(url) {
	if (getsTheFileType(url)) {
		wx.previewImage({
			urls: [url],
		});
	} else {
		previewUrl(url);
	}
}
